namespace OpenComposite.EII.Repository
{
	using System;
	using System.Collections.Generic;
	using System.Text;
	using System.Windows.Forms;

	using OpenComposite.EII.RepositoryUIs;

	using OpenComposite.Repository.Properties;

	[RepositoryItem(ItemType.RuleConstant, "rule_constants", "ruleconst_id")]
	public class RuleConstant : RepositoryItemBaseEx
	{
		#region Initialization

		public RuleConstant()
			: base("ruleconst_id", "ruleconst_nm", "ruleconst_desc", "ruleconst_ntext", Resources.sNewRuleConstant)
		{
		}

		public RuleConstant(bool bCreateNew)
			: this()
		{
			if ( bCreateNew )
				this.CreateNew();
		}

		public RuleConstant(int id)
			: this()
		{
			this.Load(id);
		}

		#endregion Initialization

		#region Public Members

		#region Properties

		/// <summary>
		/// Gets the allowed child types.
		/// </summary>
		/// <value>The allowed child types.</value>
		public override List<ItemType> AllowedChildTypes
		{
			get { return _types; }
		}

		public string DataType
		{
			get { return getString("ruleconst_datatype", "string"); }
			set { setValue("DataType", "ruleconst_datatype", value); }
		}

		public override string Display
		{
			get
			{
				return string.Format("{0} ({1})", this.Name, this.Value);
			}
		}

		/// <summary>
		/// Gets the image.
		/// </summary>
		/// <value>The image.</value>
		public override System.Drawing.Image Image
		{
			get { return Resources.png_constant; }
		}

		/// <summary>
		/// Gets the image key.
		/// </summary>
		/// <value>The image key.</value>
		public override string ImageKey
		{
			get { return "constant"; }
		}

		/// <summary>
		/// Gets the type of the repository item.
		/// </summary>
		/// <value>The type of the repository item.</value>
		public override ItemType RepositoryItemType
		{
			get { return ItemType.RuleConstant; }
		}

		/// <summary>
		/// Gets the repository item type description.
		/// </summary>
		/// <value>The repository item type description.</value>
		public override string RepositoryItemTypeDescription
		{
			get { return ""; }
		}

		/// <summary>
		/// Gets the name of the repository item type.
		/// </summary>
		/// <value>The name of the repository item type.</value>
		public override string RepositoryItemTypeName
		{
			get { return Resources.sRuleConstant; }
		}

		/// <summary>
		/// Gets the selected image.
		/// </summary>
		/// <value>The selected image.</value>
		public override System.Drawing.Image SelectedImage
		{
			get { return this.Image; }
		}

		/// <summary>
		/// Gets the selected image key.
		/// </summary>
		/// <value>The selected image key.</value>
		public override string SelectedImageKey
		{
			get { return this.ImageKey; }
		}

		/// <summary>
		/// Gets the name of the table.
		/// </summary>
		/// <value>The name of the table.</value>
		public override string TableName
		{
			get { return "rule_constants"; }
		}

		public string Value
		{
			get { return getString("ruleconst_value", null); }
			set { setValue("Value", "ruleconst_value", value); }
		}

		#endregion Properties

		#region Methods

		/// <summary>
		/// Copies this instance.
		/// </summary>
		/// <param name="deep"><c>true</c> to recursively copy the item and all subitems;
		/// <c>false</c> to copy only the item itself.</param>
		/// <returns>The copied item.</returns>
		public override IRepositoryItem Copy(bool deep)
		{
			RuleConstant copy = new RuleConstant(true);
			copy.InitializeServiceProvider(this);
			this.CopyDataRowToTarget(copy);
			return copy;
		}

		/// <summary>
		/// Gets the icon.
		/// </summary>
		/// <returns></returns>
		public override System.Drawing.Icon GetIcon()
		{
			return Resources.ico_constant;
		}

		/// <summary>
		/// Gets the value object.
		/// </summary>
		/// <returns></returns>
		public object GetValueObject()
		{
			return Global.Data.ConvertString(this.Value, this.DataType);
		}

		/// <summary>
		/// Shows the new item form.
		/// </summary>
		/// <param name="idRepository">The repository ID.</param>
		/// <param name="bAllowContinue">if set to <c>true</c> to allow save and continue.</param>
		/// <param name="parentItem">The parent item.</param>
		/// <param name="idDomain">The domain ID.</param>
		/// <param name="parentItemNew">The new parent item.</param>
		/// <returns></returns>
		public override DialogResult ShowNewItemForm(int idRepository, bool bAllowContinue,
			IRepositoryItem parentItem, ref int idDomain, out IRepositoryItem parentItemNew)
		{
			FormCreateNewRuleConstant dlg = new FormCreateNewRuleConstant(this, bAllowContinue);
			dlg.DomainID = idDomain;
			dlg.ShowAllFolders = false;
			dlg.ParentItem = parentItem;
			DialogResult result = dlg.ShowDialog();
			if ( result == DialogResult.OK || result == DialogResult.Retry ) {
				idDomain = dlg.DomainID;
				parentItemNew = dlg.NewParentItem;
			} else {
				idDomain = 0;
				parentItemNew = null;
			}
			return result;
		}

		/// <summary>
		/// Returns a <see cref="System.String"/> that represents this instance.
		/// </summary>
		/// <returns>
		/// A <see cref="System.String"/> that represents this instance.
		/// </returns>
		public override string ToString()
		{
			return string.Format("{0} ({1})", this.Name, this.Value);
		}

		#endregion Methods

		#endregion Public Members

		#region Private Members

		#region Static Fields

		private static List<ItemType> _types = new List<ItemType>();

		#endregion Static Fields

		#endregion Private Members
	}
}